var yn_jtour_var_active = false;
var yn_get_jquery_selector_is_active = false;
var styleSheet = document.createElement("style");
styleSheet.innerText = ".mouse_active{background-color: #33FFFF !important;-webkit-box-shadow: rgb(214, 216, 75) 0px 0px 5px inset !important;cursor: crosshair !important;}";
document.body.appendChild(styleSheet);
var warningDiv = document.createElement("div");
warningDiv.setAttribute('id', 'yn_jtour_form_add_slide');
warningDiv.setAttribute('style', 'width:380px;padding:20px;display:none; position:absolute; top:1px; left:1px; background-color:#7DF; border: 1px solid #444; z-index:999;');
document.body.appendChild(warningDiv);
$('div#yn_jtour_form_add_slide').html('<div style="height: 30px;line-height: 30px;">Pathname: <input type="text" id="yn_jtour_input_path_name" onClick="this.select()" style="float:right;margin-top:5px;width:300px"/><div style="height: 30px;line-height: 30px;">Top To Center: <input type="text" id="yn_jtour_input_position_top" onClick="this.select()" style="margin: 5px 20px 0 10px;width:40px"/>Left To Center: <input type="text" id="yn_jtour_input_position_left" onClick="this.select()" style="margin: 5px 10px 0 10px;width:40px"/></div><div style="height: 30px;line-height: 30px;">Selector: <input type="text" id="yn_jtour_input_selector" onClick="this.select()" style="width:300px; float:right; margin-top: 5px;" /></div><input id="yn_jtour_button_cancel" type="button" style="float:right;" value="Done" />');
var global_test;
$.fn.extend({
	path : function(root) {
		var r;
		if(root) {
			r = $(root)[0];
		} else {
			r = $()[0];
		}
		var el = this[0];
		if(el) {
			var path = "";
			while(el && el.parentNode && el != r) {
				if(el.nodeType == 9) { return; }
				if (el.id){ 
					path = ">" + el.tagName.toLowerCase() + "#" + el.id + "" + path; 
					break;
				}
				path = ">" + el.tagName.toLowerCase() + ":eq(" + $(el).prevAll(el.tagName).size() + ")" + path;
				var el = el.parentNode;
			}
		}
		return path.substring(1);
	}
});

var testclick = function(event)
{
	if(yn_jtour_var_active==true)
	{
		event.preventDefault();
		event.stopPropagation();
		var offsetY = parseInt(event.currentTarget.offsetHeight)/2;
		var offsetX = parseInt(event.currentTarget.offsetWidth)/2;
		//parseInt(!event.offsetY ? event.layerY - $(event.target).position().top : event.offsetY);
		//var offsetX = parseInt(!event.offsetX ? event.layerX - $(event.target).position().left : event.offsetX);
		leftDiv = (event.pageX + 450 > screen.width) ? screen.width - 450 : event.pageX;
		document.getElementById('yn_jtour_input_path_name').value = window.location.pathname;
		document.getElementById('yn_jtour_input_position_top').value = offsetY;
		document.getElementById('yn_jtour_input_position_left').value = offsetX;
		console.log(event);
		console.log(event.currentTarget);
		var containEle = event.currentTarget;
		//console.log($(containEle).path());
		//var containStyle = ['block', 'inline-block', 'table-cell', 'inline-table', 'inline'];
		//while(1)
		//{
			//if($(containEle).css('display') == 'block' || $(containEle).css('display') == 'inline-block')
		//	if($(containEle).css('position') == 'relative')
		//		break;
		//	containEle = containEle.parentNode;
		//	console.log($(containEle).path());
		//}
		document.getElementById('yn_jtour_input_selector').value = $(containEle).path();
		document.getElementById('yn_jtour_form_add_slide').style.top = event.pageY + 'px';
		document.getElementById('yn_jtour_form_add_slide').style.left = leftDiv + 'px';
		document.getElementById('yn_jtour_form_add_slide').style.display = 'inline';
		yn_jtour_var_active = false;
		
	}
}

var testactive = function(event)
{
	event.preventDefault();
	event.stopPropagation();
	if(yn_jtour_var_active==true)
	if(!$(event.target).hasClass('mouse_active'))
		$(event.target).addClass('mouse_active');
}

var testnotactive = function(event)
{
	event.preventDefault();
	event.stopPropagation();
	if(yn_jtour_var_active==true)
	if($(event.target).hasClass('mouse_active'))
		$(event.target).removeClass('mouse_active');
}
 
var stopFindElement = function(event)
{
	yn_get_jquery_selector_is_active = false;
	document.getElementById('yn_jtour_form_add_slide').style.display = 'none';
	$('.mouse_active').removeClass('mouse_active');
	$('input#yn_jtour_button_cancel').unbind('click', stopFindElement);
	$('html>body div,a,input,p,strong').unbind('mousedown', testclick);
	$('html>body div,a,input,p,strong').unbind('mouseover', testactive);
	$('html>body div,a,input,p,strong').unbind('mouseout', testnotactive);
	event.preventDefault();
	event.stopPropagation();
}